package com.eshop.eshopmanagersystem.controller;

import com.eshop.eshopmanagersystem.dal.*;
import com.eshop.eshopmanagersystem.model.BarcodeInfo;
import com.eshop.eshopmanagersystem.model.OrderInfo;
import com.eshop.eshopmanagersystem.model.UserInfo;
import com.eshop.eshopmanagersystem.unti.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * 智能电子超市管理后端API
 *
 */

@RestController
public class ManagerController {

    //管理信息
    @Autowired
    ManagerDAL mdal;

    //管理用户
    @Autowired
    UserDAL userDAL;

    //系统基本信息
    @Autowired
    SystemDAL systemDAL;

    //订单管理
    @Autowired
    OrderDAL orderDAL;

    //报表管理
    @Autowired
    EchartsDAL echartsDAL;

    /**
     * 查询系统基本信息
     *
     */
    @CrossOrigin
    @GetMapping(value = "/getSystemInfo",produces = "application/json; utf-8")
    public Response getSystemInfo(){
        return new Response(200,"查询成功",systemDAL.getSystemInfo());
    }

    /**
     * 管理端后台显示产品分页数据
     *
     */
    @CrossOrigin
    @RequestMapping(value = "/getProductByPager", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response getProductByPager(@RequestBody Pager pager){
        if(pager.getName().trim().equals("")||pager.getName()==null){
            //不带条件查询
            System.out.println("不带条件查询");
            return new Response(200,"查询成功",mdal.getProductDataByPager(pager));
        }else {
            //带条件查询
            System.out.println("带条件查询");
            return new Response(200,"查询成功",mdal.getProductDataByPagerAndKeyname(pager));
        }
    }

    /**
     * 管理后台产品列表编辑API
     * 参数是产品对象
     */
    @CrossOrigin
    @RequestMapping(value = "/editProduct", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response editProduct(@RequestBody BarcodeInfo barcodeInfo){
        return new Response(200,"编辑成功",mdal.editProductById(barcodeInfo));
    }

    /**
     * 管理后台添加商品时，通过条形码判断商品是否存在
     * 存在返回true / 不在返回false
     */
    @CrossOrigin
    @RequestMapping(value = "/validateProductIsExist", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response validateProductIsExist(@RequestBody BarcodeInfo barcodeInfo){
        return new Response(200,"查询成功",mdal.validateProductIsExist(barcodeInfo));
    }

    /**
     * 管理后台添加商品API
     * 返回值为true添加成功/false添加失败
     */
    @CrossOrigin
    @RequestMapping(value = "/addProduct", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response addProduct(@RequestBody BarcodeInfo barcodeInfo){
        return new Response(200,"执行添加",mdal.addProduct(barcodeInfo));
    }

    /**
     * 管理后台批量删除商品API
     * 参数是要删除数据的id集合
     */
    @CrossOrigin
    @RequestMapping(value = "/deleteProduct", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response deleteProduct(@RequestBody BarcodeInfo barcodeInfo){
        //System.out.println(barcodeInfo.getIds());
        return new Response(200,"执行删除成功",mdal.deleteProduct(barcodeInfo));
    }

    /**
     * 后台管理员登录请求
     *
     */
    @CrossOrigin
    @RequestMapping(value = "/validateLogin", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response validateLogin(@RequestBody UserInfo user){
        UserInfo loginuser=userDAL.validateUser(user);
        if(loginuser!=null) {
            String token = TokenUtils.sign(user);
            HashMap<String, Object> hs = new HashMap<>();
            hs.put("token", token);
            hs.put("user",loginuser.getUsername());
            hs.put("role",loginuser.getUser_role());
            hs.put("user_id",loginuser.getUser_id());
            return new Response(200, "请求成功", hs);
        }else{
            return new Response(403,"验证失败",null);
        }
    }

    /**
     * 后台订单管理 分页请求方法/带搜索条件的分页请求方法
     *
     */
    @CrossOrigin
    @RequestMapping(value = "/getOrderByPager", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response getOrderByPager(@RequestBody Pager pager){
        if(pager.getOrder_code().trim().equals("")||pager.getOrder_code()==null){
            //不带条件查询
            System.out.println("不带条件查询");
            return new Response(200,"查询成功",orderDAL.getOrderDataByPager(pager));
        }else {
            //带条件查询
            System.out.println("带条件查询");
            return new Response(200,"查询成功",orderDAL.getOrderDataByPagerAndOrderCode(pager));
        }
    }

    /**
     * 后台订单管理 查看详细订单详情时获取订单与产品关系数据
     *
     */
    @CrossOrigin
    @RequestMapping(value = "/getOrderDetails", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response getOrderDetails(@RequestBody OrderInfo orderInfo){
        return new Response(200,"查询成功",orderDAL.getOrderDetailsByOrderID(orderInfo));
    }

    /**
     * 后台用户管理 列表分页请求API
     */
    @CrossOrigin
    @RequestMapping(value = "/getUsersByPager", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response getUsersByPager(@RequestBody Pager pager){
        if(pager.getUsername().trim().equals("")||pager.getUsername()==null){
            //不带条件查询
            System.out.println("不带条件查询");
            return new Response(200,"查询成功",userDAL.getUsersDataByPager(pager));
        }else {
            //带条件查询
            System.out.println("带条件查询");
            return new Response(200,"查询成功",userDAL.getUsersDataByPagerAndUserName(pager));
        }
    }

    /**
     * 编辑用户信息
     * 参数：user_id user_manager等基础用户数据
     */
    @CrossOrigin
    @RequestMapping(value = "/editUser", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response editUser(@RequestBody UserInfo userInfo){
        return new Response(200,"编辑成功",userDAL.editUser(userInfo));
    }

    /**
     * 重置用户密码
     * 参数： user_id
     */
    @CrossOrigin
    @RequestMapping(value = "/resetUserPass", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response resetUserPass(@RequestBody UserInfo userInfo){
        String resetPass = RandomPassHelper.getPassWordOne(8);
        userInfo.setUser_password(EncryptHelper.SHA(resetPass));
        if(userDAL.updateUserPass(userInfo)){
            return new Response(200,"密码重置成功",resetPass);
        }else {
            return new Response(500,"密码重置失败",null);
        }
    }

    /**
     * 用户修改自己密码
     * 参数：username , oldpass , newpass
     */
    @CrossOrigin
    @RequestMapping(value = "/editUserPass", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response editUserPass(@RequestBody UserInfo userInfo){
        if(userDAL.editUserPass(userInfo)){
            return new Response(200,"密码修改成功",null);
        }else{
            return new Response(500,"密码修改失败",null);
        }
    }

    /**
     * 管理后台添加用户
     */
    @CrossOrigin
    @RequestMapping(value = "/addUser", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response addUser(@RequestBody UserInfo userInfo){
        if(userDAL.addUser(userInfo)){
            return new Response(200,"用户添加成功",null);
        }else {
            return new Response(500,"用户添加失败",null);
        }
    }

    /**
     * 管理后台批量删除用户API
     * 参数是要删除数据的id集合
     */
    @CrossOrigin
    @RequestMapping(value = "/deleteUser", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response deleteUser(@RequestBody UserInfo userInfo){
        if(userDAL.deleteUser(userInfo)){
            return new Response(200,"删除成功",null);
        }else{
            return new Response(500,"删除失败",null);
        }
    }

    /**
     * 管理后台批量删除订单数据API
     * 参数是要删除数据的id集合
     */
    @CrossOrigin
    @RequestMapping(value = "/deleteOrder", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response deleteOrder(@RequestBody OrderInfo orderInfo){
        if(orderDAL.deleteOrder(orderInfo)){
            return new Response(200,"删除成功",null);
        }else{
            return new Response(500,"删除失败",null);
        }
    }

    /**
     * 数据报表
     */
    @CrossOrigin
    @RequestMapping(value = "/getEchartsData", method = RequestMethod.GET, produces = "application/json; utf-8")
    public Response getEchartsDatagetEchartsData(){
        return new Response(200,"查询成功",echartsDAL.getEchartsData());
    }
}
